package cn.itcast.dewu.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.json.JSONUtil;
import cn.itcast.dewu.dto.Result;
import cn.itcast.dewu.pojo.Goods;
import cn.itcast.dewu.service.GoodsService;
import cn.itcast.dewu.utils.RedisIdWork;
import cn.itcast.dewu.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import static cn.itcast.dewu.utils.RedisConstants.CACHE_GOODS_KEY;
import static cn.itcast.dewu.utils.RedisConstants.GOODS_STOCK_KEY;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisIdWork idWork;

    /*
     * 查看某个商品详情
     * */
    @GetMapping("/{id}")
    public Result queryGoodsByID(@PathVariable("id") Long id) {
        return goodsService.queryGoodsByID(id);
    }

    //更新商品
    @PutMapping
    public Result updateGoods(@RequestBody Goods goods) {
        return goodsService.update(goods);
    }

    //根据id删除商品
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return goodsService.deleteGoodsById(id);
    }

    /*
     * 增加商品
     * */
    @PostMapping("/save")
    public Result saveGoods(@RequestBody Goods goods) {
        return goodsService.saveGoods(goods);
    }

    /*
     * 查看所有商品信息
     * 参数 : 当前页码
     * */
    @GetMapping("/list/{corrent}")
    public Result queryGoodsList(@PathVariable("corrent") Integer corrent) {
        return goodsService.queryGoodsList(corrent);
    }


    /*
    * 搜索商品
    * */
    @GetMapping("/query/GoodsByContent/{corrent}")
    public Result queryblogByContent(@PathVariable("corrent")Integer corrent,@RequestParam("content") String content) {
        return goodsService.queryGoodsByContent(corrent,content);
    }

    /*
     * 查看自己的商品
     * */
    @GetMapping("/mygoods")
    public Result queryMyGoods() {
        return goodsService.queryMyGoods();
    }

}
